<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<style type="text/css">
#systemConfig_dlg{
	padding:10px 20px;
}
#systemConfig_form table{
	width:100%;
	border-collapse:collapse;
	border-right:1px solid #A9A9A9;
}
#systemConfig_form th{
	border-left:1px solid #A9A9A9;
	border-top:1px solid #A9A9A9;
	border-bottom:1px solid #A9A9A9;
	border-right:1px solid #D3D3D3;
	padding-left:10px;
	padding-right:10px;
	text-align:right;
}
#systemConfig_form td{
	border-top:1px solid #A9A9A9;
	border-bottom:1px solid #A9A9A9;
	padding-top:5px;
	padding-bottom:5px;
	padding-left:5px;
	text-align:left;
}
</style>

<script type="text/javascript">
var systemConfigTable = 'systemConfig';
var systemConfigQuery;
var wysiwye = false;	// what you see is what you export

$(function() {
	initDataGrid('#systemConfig_grid', 'search/' + systemConfigTable + '.json');
	systemConfigQuery = '';

	initUploadFileForm($('#systemConfig_file_form'), $('#systemConfig_columnMap'), $('#systemConfig_grid'), wysiwye);
});


/**
 * 查询
 */
function searchSystemConfig() {
	systemConfigQuery = getSystemConfigQueryJson();
	searchDataGrid('#systemConfig_grid', systemConfigQuery);
}

/**
 * 查询条件
 */
function getSystemConfigQueryJson(){
	var query = {
		"items":[
		    {
				"field": "name",
				"operator": "like",
				"value": $('#systemConfig_name').val()
			},		    {
				"field": "value",
				"operator": "like",
				"value": $('#systemConfig_value').val()
			},		    {
				"field": "description",
				"operator": "like",
				"value": $('#systemConfig_description').val()
			}		]	}

	return query;
}

/**
 * 刷新
 */
//function refreshSystemConfig() {
//	refreshDataGrid('#systemConfig_grid');
//}

/** 
 * 添加
 */
function addSystemConfigItem(){
	$('#systemConfig_name_input').prop('readonly', false);
	showAddDialog('#systemConfig_form', '#systemConfig_dlg');
	
	// 设置每一个radio最后一项选中
	$('#systemConfig_form').children().find(':radio').each(function(){
		$(this).prop('checked', true);
	});
	
	$('#systemConfig_save').unbind('click');
	$('#systemConfig_save').click(function(){
	 	var valid = true;
	 	valid = checkExistence(systemConfigTable, '数据已存在', 'name', $('#systemConfig_name_input').val());
	 	
	 	if(valid){
	 		
			$('#systemConfig_id_input').numberbox('setValue', '0');
			saveItem({jgridId:'#systemConfig_grid', jformId:'#systemConfig_form', jdlgId:'#systemConfig_dlg', table:systemConfigTable, callback:null});
			clearCache();
	 	}
 	});
}

/**
 * 编辑
 */
function editSystemConfigItem(){
	var flag = showEditDialog('#systemConfig_grid', '#systemConfig_form', '#systemConfig_dlg');
 
	if(flag){
		$('#systemConfig_name_input').prop('readonly', true);
		
	 	var valid = true;
	 	/* 如果要进行数据重复校验，在这里添检查ajax调用
	 	 * 以下例子用来检查单个字段是否重复
	 	 */
	 	//valid = checkExistence(systemConfigTable, '数据已存在', <fieldName>, $('#systemConfig_<fieldName>_input').val());
	 	
	 	if(valid){
			$('#systemConfig_save').unbind('click');
			$('#systemConfig_save').click(function(){
				saveItem({jgridId:'#systemConfig_grid', jformId:'#systemConfig_form', jdlgId:'#systemConfig_dlg', table:systemConfigTable, callback:null});
				clearCache();
			});
	 	}
	}
}

/**
 * 复制
 */
function copySystemConfigItem(){
	var flag = showCopyDialog('#systemConfig_grid', '#systemConfig_form', '#systemConfig_dlg');
	
	if(flag){
		$('#systemConfig_name_input').prop('readonly', false);
		
 		/* 在这里重设需要初始化的字段 */
 		// 默认清空自增主键
		_.each(['id'], function(element){
			$('#systemConfig_form').children().find('input[name="' + element + '"]').each(function(){
				$(this).val('');
			});
		});
		

		$('#systemConfig_save').unbind('click');
		$('#systemConfig_save').click(function(){
		 	var valid = true;
		 	valid = checkExistence(systemConfigTable, '数据已存在', 'name', $('#systemConfig_name_input').val());
		 	
		 	if(valid){
		 		$('#systemConfig_id_input').numberbox('setValue', '0');
		 		saveItem({jgridId:'#systemConfig_grid', jformId:'#systemConfig_form', jdlgId:'#systemConfig_dlg', table:systemConfigTable, callback:null});
		 		clearCache();
		 	}
		});
	}
}



/**
 * 导出查询结果
 */
function exportSystemConfigQuery(type){
	var suffix = type == 'Excel' ? 'xls' : 'csv';
	
	showConfirmMessage('是否以' + type + '格式导出查询结果?', function(r){
		if(r){
			var form = new Object();

			form.columns = getExportColumns($('#systemConfig_grid'), wysiwye);
			form.table = systemConfigTable;
			
			var query = systemConfigQuery ? systemConfigQuery : getSystemConfigQueryJson();
			form.query = JSON.stringify(query);
			
			// 表格显示的名称
			form.tableDisplayName = $(jtabId).tabs('getSelected').panel('options').title;
			// 下载文件的名称
			form.fileName = $(jtabId).tabs('getSelected').panel('options').title;
			// 下载文件格式
			form.type = suffix;
			
			downloadFile(form);
		}
	});
}


function resizeSystemConfig(){
	$('#systemConfig_grid').datagrid('resize');
}

function clearCache(){
	$.ajax({
		type: "post",
		url: 'http://' + window.location.hostname + '/jiayida-mobile/cache/clear.json'
	});
}
</script>

<table id="systemConfig_grid" data-options="toolbar:'#systemConfig_toolbar'">
    <thead>
        <tr>
        	<th data-options="field:'ck'" checkbox="true"></th>
			<th data-options="field:'id',hidden:false">编号</th>
			<th data-options="field:'name',hidden:false">键名</th>
			<th data-options="field:'value',hidden:false">键值</th>
			<th data-options="field:'description',hidden:false">说明</th>
		</tr>
    </thead>
</table>

<div id="systemConfig_toolbar">
	<div style="display:none">
		<form id="systemConfig_file_form" enctype="multipart/form-data" method="post" action="file/import.json">
			<input id="systemConfig_file" type="file" name="file" accept="" onchange="startUpload($('#systemConfig_file_form'));"/>
			<input name="table" type="text" value="systemConfig">
			<input id="systemConfig_columnMap" name="columnMap" type="text">
			<input id="systemConfig_file_submit" type="submit"/>
		</form>
		<iframe id="systemConfig_file_target" name="systemConfig_file_target"></iframe>
	</div>

	<div id="systemConfig_querybar" title="查询条件" class="easyui-panel" data-options="collapsible:true,border:false,onCollapse:resizeSystemConfig,onExpand:resizeSystemConfig">
		<table>
			<tr>
				<td style="text-align:right;">键名</td>
				<td style="text-align:left;padding-left:0px;padding-right:10px;">
					<input id="systemConfig_name" style="width:100%" type="text"/>
				</td>
				<td style="text-align:right;">键值</td>
				<td style="text-align:left;padding-left:0px;padding-right:10px;">
					<input id="systemConfig_value" style="width:100%" type="text"/>
				</td>
				<td style="text-align:right;">说明</td>
				<td style="text-align:left;padding-left:0px;padding-right:10px;">
					<input id="systemConfig_description" style="width:100%" type="text"/>
				</td>
				<td style="padding-left:10px;">
					<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="searchSystemConfig()">查询</a>
					<a href="#" class="easyui-linkbutton" iconCls="icon-clear" onclick="clearCriteria('#systemConfig_querybar')">清空</a>
				</td>
			</tr>
		</table>
	</div>

	<div style="margin-bottom:5px">
  		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addSystemConfigItem()">添加</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editSystemConfigItem()">编辑</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-copy" plain="true" onclick="copySystemConfigItem()">复制</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-cross" plain="true" onclick="deleteItem({jgridId:'#systemConfig_grid', table:systemConfigTable, idField:'id', callback:function(){}})">删除</a>
        <a href="#" class="easyui-menubutton" iconCls="icon-export" data-options="menu:'#systemConfig_export'">导出</a>
        <div id="systemConfig_export">
			<div data-options="iconCls:'icon-excel'" onclick="exportSystemConfigQuery('Excel')">Excel</div>
			<div data-options="iconCls:'icon-csv'" onclick="exportSystemConfigQuery('CSV')">CSV</div>
		</div>
        <a href="#" class="easyui-menubutton" iconCls="icon-import" data-options="menu:'#systemConfig_import'">导入</a>
        <div id="systemConfig_import">
			<div data-options="iconCls:'icon-excel'" onclick="selectFile('#systemConfig_file', 'Excel')">Excel</div>
			<div data-options="iconCls:'icon-csv'" onclick="selectFile('#systemConfig_file', 'CSV')">CSV</div>
		</div>
	</div>
</div>

<div id="systemConfig_dlg" class="easyui-dialog" style="width:40%" data-options="shadow:false,resizable:true,closed:true" buttons="#systemConfig_dlg_buttons">
     <form id="systemConfig_form" method="post" novalidate>
     	<table>
					
			<tr style="display:none">
				<th style="">
					编号
				</th>
				<td><input id="systemConfig_id_input" style="width:95%" name="id" class="easyui-numberspinner" data-options="required:false"></td>
				<td><span class="hint"></span></td>
		</tr>
					
			<tr style="">
				<th style="">
					键名
				</th>
				<td><input id="systemConfig_name_input" style="width:95%" name="name" class="easyui-validatebox" data-options="required:false"></td>
				<td><span class="hint"></span></td>
		</tr>
				
			<tr style="">
				<th style="">
					键值
				</th>
				<td><input id="systemConfig_value_input" style="width:95%" name="value" class="easyui-validatebox" data-options="required:false"></td>
				<td><span class="hint"></span></td>
		</tr>
				
			<tr style="">
				<th style="">
					说明
				</th>
				<td><input id="systemConfig_description_input" style="width:95%" name="description" class="easyui-validatebox" data-options="required:false"></td>
				<td><span class="hint"></span></td>
		</tr>
		</table>
     </form>
 </div>
 <div id="systemConfig_dlg_buttons">
     <a id="systemConfig_save" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-save">保存</a>
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-no" onclick="closeDialog('#systemConfig_dlg')">取消</a>
 </div>



